package com.minyifang.wcspf.work.ui.user.activities;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseMvpActivity;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.common.utils.ToastHelper;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.model.IntegralBean;
import com.minyifang.wcspf.work.ui.user.presenter.MyIntegralPresenter;
import com.minyifang.wcspf.work.ui.user.view.MyIntegralView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by LuoYaWei on 2018/6/25.
 */
public class MyIntegralActivity extends BaseMvpActivity<MyIntegralView, MyIntegralPresenter> implements MyIntegralView {

    @BindView(R.id.tv_integral)
    TextView tv_integral;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private MyIntegralAdapter mAdapter;
    private List<IntegralBean> mList;

    @Override
    protected MyIntegralPresenter createPresenter() {
        return new MyIntegralPresenter();
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_my_integral;
    }

    @Override
    protected void initView() {

        mList = new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mAdapter = new MyIntegralAdapter(R.layout.item_my_integral, mList);
        mRecyclerView.setAdapter(mAdapter);

    }

    @OnClick(R.id.tv_get_integral)
    void getIntegral() {
        startActivity(new Intent(this, DailySignActivity.class));
    }

    @Override
    protected void initData() {
        mPresenter.getIntegralData();
    }

    @Override
    public void showIntegralData(HttpRespond<String> respond) {
        if (respond.result == 1) {
            if (respond.isaes == 1) {
                try {
                    JSONObject object = new JSONObject(SafeUtils.decrypt(respond.data));
                    LogUtil.e("积分数据" + SafeUtils.decrypt(respond.data));
                    tv_integral.setText(object.getString("allintegral"));

                    List<IntegralBean> list = new Gson().fromJson(object.getString("integrallist"), new TypeToken<List<IntegralBean>>() {
                    }.getType());
                    mList.clear();
                    mList.addAll(list);
                    mAdapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    LogUtil.e("积分数据异常");
                    e.printStackTrace();
                }
            }

        } else
            ToastHelper.getInstance(this).showWarn(respond.message);

    }

    class MyIntegralAdapter extends BaseQuickAdapter<IntegralBean, BaseViewHolder> {

        public MyIntegralAdapter(int layoutResId, @Nullable List<IntegralBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, IntegralBean item) {
            helper.setText(R.id.item_content, item.getChangetype());
            helper.setText(R.id.item_date, item.getChangedatetime());
            helper.setText(R.id.item_integral, item.getChange());
        }
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingDialog();
    }
}
